package com.tang;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class jwtText {
    public static void main(String[] args) {

        //获取系统的当前时间
        long currentTimeMillis = System.currentTimeMillis();
        Date date = new Date(currentTimeMillis);

        //生成jwt令牌
        JwtBuilder builder = Jwts.builder()
                .setId("66") //设置jwt的编码
                .setSubject("大哥大哥")//设置jwt主题
                .setIssuedAt(new Date())//设置jwt签发日期
                //.setExpiration(date)//设置jwt的过期时间
                .claim("roles","admin")
                .claim("comoany","tang")
                .signWith(SignatureAlgorithm.HS256,"tang");

        //生成jwt
        String compact = builder.compact();
        System.out.println(compact);  //eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NiIsInN1YiI6IuWkp-WTpeWkp-WTpSIsImlhdCI6MTU5NDgzMTY2OCwicm9sZXMiOiJhZG1pbiIsImNvbW9hbnkiOiJ0YW5nIn0.ymOpjl218m0qjymwGeEs73MpLA_5lINzHrxlBJcFJUI


        ////解析jwt,得到其内部的数据
         Claims claims = Jwts.parser().setSigningKey("tang").parseClaimsJws(compact).getBody();
             System.out.println(claims);
    }
}
